import React from 'react';
import MomentsCard from './momentsCard.jsx';
import './moments.scss';
import { newsList } from 'api/moments.js';
import Footer from '../widget/footer/footer.jsx';
class Moments extends React.Component{
	constructor (props) {
		super(props)
		this.state={
			company_logo: '',
			company_name: '',
			companyInfo: [],
			currentPage: 0,
      lastPage: 0
		}
	}
	_newsList (num) {
		newsList({
			employee_id: 1,
			company_id: sessionStorage.getItem('company_id'),
			page_size: 6,
			page: num
		}).then(res => {
			this.setState({
				company_logo: res.data.company_logo,
				company_name: res.data.company_name,
				companyInfo: [...this.state.companyInfo, ...res.data.data],
				currentPage: num,
      	lastPage: res.data.last_page
			})
		})
	}
	componentDidMount(){
		document.title = '动态'
		this._newsList(1);
		let that=this;
    let i=1;
    window.onscroll= function(){
      var t = document.documentElement.scrollTop||document.body.scrollTop;
      var b = document.documentElement.scrollHeight||document.body.scrollHeight;
      var h = document.documentElement.clientHeight||document.body.clientHeight;
      if((+t+h) == b && i <= that.state.lastPage && t!==0){
        that._newsList(++i)
      }
    }
	}
	render() {
		return(
			<div className='moments'>
				{/*<div className="title">
					<div><i className="glyphicon glyphicon-close"></i></div>
					<div>动态</div>
					<div><i className="glyphicon glyphicon-options"></i></div>
				</div>*/}
				<div className="moments_content">
				{
					this.state.companyInfo.map((item,index) => {
						return(
							<MomentsCard key={index} company_logo={this.state.company_logo} company_name={this.state.company_name} companyInfo={{...item}}/>
						)
					})
				}
				</div>
				<div className={this.state.currentPage >= this.state.lastPage ? 'noMore' : 'hiden'} style={{textAlign: 'center'}}>————  没有更多动态！ ————</div>
				<Footer/>
			</div>
			)
	}
}
export default Moments;
